<?php
/**
 * Created by LíJimpéy(：
 * User: jimpey88
 * Date: 2024/3/26
 * Email: < jimpey@qq.com >
 * Project: company-admin-tp6
 */

namespace app\backend_api;

use app\backend_api\exception\AdminApiException;
use app\common\Services\JsonService;
use app\ExceptionHandle;
use think\exception\HttpException;
use think\exception\ValidateException;
use think\Response;
use Throwable;

class BackEndApiHandle extends ExceptionHandle
{
    use JsonService;

    /**
     * 不需要记录信息（日志）的异常类列表
     * @var array
     */
    protected $ignoreReport = [
        AdminApiException::class,
    ];

    public function render($request, Throwable $e): Response
    {
        // 参数验证错误
        if ($e instanceof ValidateException || $e instanceof AdminApiException) {
            self::throw($e->getMessage());
        }
        // 请求异常
        if ($e instanceof HttpException && $request->isAjax()) {
            return response($e->getMessage(), $e->getStatusCode());
        }

        // 其他错误交给系统处理
        return parent::render($request, $e);
    }


}